package com.zzg.opencv;

import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
import org.opencv.videoio.VideoCapture;

import com.zzg.opencv.util.Mat2BufferedImage;

public class Camera {
	static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);  //加载动态链接库
    }
	
	private JFrame frame;
	static JLabel label;
	static int flag=0;
	
	public Camera(){
		initialize();
	}
	
	// widows 窗口初始化
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 798, 600);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
				
		JButton btnNewButton = new JButton("拍照");
		btnNewButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				flag=1;
			}
		});
		btnNewButton.setBounds(20, 20, 113, 27);
		frame.getContentPane().add(btnNewButton);
		
		label=new JLabel("");
		label.setBounds(50, 50, 640, 480);
		frame.getContentPane().add(label);
	}

	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Camera window = new Camera();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		//操作
		VideoCapture camera=new VideoCapture();
		camera.open(1);
		if(!camera.isOpened()){
			System.out.println("Camera Error");
		}
		else{
			Mat frame=new Mat();
			while(flag==0){
				camera.read(frame);
				Mat gray=new Mat(frame.rows(),frame.cols(),frame.type());
				Imgproc.cvtColor(frame, gray, Imgproc.COLOR_RGB2GRAY);
				label.setIcon(new ImageIcon(Mat2BufferedImage.matToBufferedImage(gray)));
				try{
					Thread.sleep(100);
				}
				catch(InterruptedException e){
					e.printStackTrace();
				}
			}
		}
	}

}
